package com.linkage.usercenter.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.linkage.common.base.RequestInfo;
import com.linkage.common.base.RespAppVO;
import com.linkage.common.constant.CommonSysConstant;
import com.linkage.common.util.RedisUtils;
import com.linkage.usercenter.config.TokenCheckException;
import com.linkage.usercenter.dao.CoreCardInfoMapper;
import com.linkage.usercenter.dao.CorePersonInfoMapper;
import com.linkage.usercenter.dao.CoreUserMapper;
import com.linkage.usercenter.feign.HomeManageFeign;
import com.linkage.usercenter.pojo.entity.CoreCardInfoWithBLOBs;
import com.linkage.usercenter.pojo.entity.CorePersonInfo;
import com.linkage.usercenter.pojo.entity.CoreUser;
import com.linkage.usercenter.pojo.vo.*;
import com.linkage.usercenter.service.IuserService;
import com.linkage.usercenter.service.IuserToolService;
import com.linkage.usercenter.utils.TableCommonDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserToolServiceImpl implements IuserToolService {

    private static final Logger logger = LoggerFactory.getLogger(UserToolServiceImpl.class);

    @Value("${customer.token-config.token-time}")
    private int tokenValidSecond;
    @Autowired
    private RedisUtils redisUtils;



    //依据TOKEN获取用户信息
    @Override
    public CoreUser getUserInfoByToken(RequestInfo reqInfo) {
        String usertoken = reqInfo.getBase().getUsertoken();
        if (StrUtil.isNotBlank(usertoken)) {
            try {
                String userInfoTokenString = (String) redisUtils.get(usertoken);
                if (StrUtil.isNotBlank(userInfoTokenString)) {
                    redisUtils.expire(usertoken, tokenValidSecond);
                    return JSONUtil.toBean(userInfoTokenString, CoreUser.class);
                }
            } catch (Exception e) {
                logger.error("redis缓存获取用户信息出错！", e);
                throw new TokenCheckException();
            }
        }
        throw new TokenCheckException();
    }

}
